package com.book.common.runner;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.stereotype.Component;

import java.net.InetAddress;

/**
 * Description：SpringBoot启动之后
 * @Author： leo.xiong
 * @CreateDate： 2019/08/20 14:23
 * @Email： leo.xiong@suyun360.com
 * Version：1.0
 */
@Slf4j
@Component
public class SpringBootStartUpRunner implements ApplicationRunner {

    @Autowired
    protected ConfigurableApplicationContext context;

    @Value("${server.port:8080}")
    protected String port;

    @Value("${server.servlet.context-path:}")
    protected String contextPath;

    @Value("${spring.application.name:}")
    protected String name;

    @Value("${custom.swagger:true}")
    protected Boolean isSwagger;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        if (context.isActive()) {
            InetAddress address = InetAddress.getLocalHost();
            String url = String.format("http://%s:%s", address.getHostAddress(), port);
            if (StringUtils.isNotBlank(contextPath)) {
                url += contextPath;
            }
            log.info("  _____   ________     ____     ______     ________  ");
            log.info(" / ____\\ (___  ___)   (    )   (   __ \\   (___  ___) ");
            log.info("( (___       ) )      / /\\ \\    ) (__) )      ) )    ");
            log.info(" \\___ \\     ( (      ( (__) )  (    __/      ( (     ");
            log.info("     ) )     ) )      )    (    ) \\ \\  _      ) )    ");
            log.info(" ___/ /     ( (      /  /\\  \\  ( ( \\ \\_))    ( (     ");
            log.info("/____/      /__\\    /__(  )__\\  )_) \\__/     /__\\    ");
            log.info("                                                      ");
            log.info("{} 启动完毕，地址：{}", name, url);
            if (isSwagger) {
                log.info("{} 接口文档，地址：{}{}", name, url, "swagger-ui.html");
            }
        }
    }
}
